//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<GameApp.h>
///	@path	~/src/appear/xwin/
///	@date	2007/07/03
///	@desc	.

#pragma once

#include "xeres.h"

#include "lib/system/lock.h"
#include "lib/utilities/dep_stages.h"

#include "appear/app/input_handler.h"
#include "appear/app/ticker.h"
#include "appear/app/message_filter.h"

#include <deque>

namespace xeres {

	// forward declaration
	class Engine;

	/*!
		\class	GameApp
		\brief	Game window application class.
	*/
	class GameApp
	{
	public:

		/// \ctor
		GameApp( void );

		/// \dtor
		virtual ~GameApp( void );

	public:

	//@ interface

		/*!
			\brief		Call to perform application initialization.
						Usually application should load settings and
						prepare resources at this time.
			\return		Result of initialization.
						If failed, application will terminate.
		*/
		virtual bool Initialize( void );

		/*!
			\brief		Clean up application & save settings or informations.
		*/
		virtual void Finalize( void );

	//@ local

		/*!
			\brief		Run main game loop.
		*/
		void Run( void * primaryWindow );

		/*!
			\brief		Add message filter.
		*/
		bool AddFilter( const WString& name , const WString& dep , MessageFilter * filter );

		/*!
			\brief		Add tick filter.
		*/
		bool AddTicker( const WString& name , const WString& dep , Ticker * ticker );

	protected:

		/*!
			\brief		Get engine instance, only for derived child use.
		*/
		Engine& engine( void );

	private:

		// message process func
		static LRESULT CALLBACK MessageProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );

		// run filter
		bool RunFilter( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );

		// run ticker
		void RunTicker( void );

	//@ data

		// atom for proc tags
		static ATOM						s_procAtom;

		// old wnd proc
		WNDPROC							m_oldProc;

		bool							m_msgFilterDirt;
		bool							m_tickerDirt;

		// message handling

		dep_stages< MessageFilter * >	m_msgFiltersStage;
		std::deque< MessageFilter * >	m_msgFilters;

		// ticker

		dep_stages< Ticker * >			m_tickersStage;
		std::deque< Ticker * >			m_tickers;
	};

} // namespace xeres
